#include "Character.h"

using namespace std;

Character::Character(void)
{
	positionX=1;
	positionZ=1;
	direction = NONE;
}


Character::~Character(void)
{
}

Character :: Character(float x, float z,float angle_,float width, Field *field){
	positionX=x;
	positionZ=z;
	angle = angle_;
	direction = NONE;
	this->field = field; 
	this->width = width;
}


void Character :: setDirection(int dir){direction = dir;}
int Character :: getDirection(){return direction;}

int Character :: getAbsoluteX(){return positionX+0.2;}
int Character :: getAbsoluteZ(){return positionZ+0.2;}

int Character :: getRoundedX(){
	int buff = (int)(positionX*10)%10;
	if(buff<5)
		return (int)positionX;
	else
		return (int)(positionX+1);
}

int Character :: getRoundedZ(){
	int buff = (int)(positionZ*10)%10;
	if(buff<5)
		return (int)positionZ;
	else
		return (int)(positionZ+1);
}

bool Character :: isInTheMiddle(){
	int buffX = positionX*1000 ; 
	int buffZ = positionZ*1000;
	return ((buffX % 1000 == 0 || buffX % 1000 == 999)  && (buffZ % 1000 == 0 || buffZ % 1000 == 999 ));
}

bool Character :: isMovable(int direction){
	switch(direction)
	{
	case NONE :
		return true;
	case UP :
		{
			if(getRoundedZ() <= 0)
				return true;
			return !field->isAWall((int)(getRoundedX()),(int)(getRoundedZ()-1));
		}
	case DOWN :
		{
			if(getRoundedZ() >= field->getHeight()-1)
				return true;
			return !field->isAWall((int)(getRoundedX()),(int)(getRoundedZ()+1));
		}
	case LEFT :
		{
			if(getRoundedX() <=0)
					return true;
			return !field->isAWall((int)(getRoundedX()-1),(int)(getRoundedZ()));
		}
	case RIGHT :
		{
			if(getRoundedX() >= field->getWidth()-1)
				return true;
			return !field->isAWall((int)(getRoundedX()+1),(int)(getRoundedZ()));
		}
	}
	return false;
}

bool Character :: isMovableWithoutTwisted(int direction){
	switch(direction)
	{
	case NONE :
		return true;
	case UP :
		{
			if(getRoundedZ() <= 0)
				return true;
			return !field->isAConcreteWall((int)(getRoundedX()),(int)(getRoundedZ()-1));
		}
	case DOWN :
		{
			if(getRoundedZ() >= field->getHeight()-1)
				return true;
			return !field->isAConcreteWall((int)(getRoundedX()),(int)(getRoundedZ()+1));
		}
	case LEFT :
		{
			if(getRoundedX() <= 0)
					return true;
			return !field->isAConcreteWall((int)(getRoundedX()-1),(int)(getRoundedZ()));
		}
	case RIGHT :
		{
			if(getRoundedX() >= field->getWidth()-1)
				return true;
			return !field->isAConcreteWall((int)(getRoundedX()+1),(int)(getRoundedZ()));
		}
	}
	return false;
}

float Character:: getWidth(){
	return width;
}

float Character ::getX(){
	return positionX;
}

float Character :: getZ(){
	return positionZ;
}

float Character :: getAngle(){
	return angle;
}

void Character :: adjustAngle (){
	if(angle >= 360)
		angle -= 360 ;
	else if(angle < 0)
		angle = 360 + angle;
}

void Character :: adjustPosition(){
	if(positionX < -0.5)
		positionX = field->getWidth() - 0.6;
	else if(positionX > field->getWidth() - 0.5)
		positionX = -0.4;
	else if (positionZ < -0.5)
		positionZ = field->getHeight() - 0.6;
	else if(positionZ > field->getHeight() - 0.5)
		positionZ = -0.4;
}